package com.wiscamp.ninechapters.panels.adapter.resources;

import com.wiscamp.ninechapters.panels.application.command.CreateMenuCommand;
import com.wiscamp.ninechapters.panels.application.command.UpdateMenuCommand;
import com.wiscamp.ninechapters.panels.application.services.MenuAppService;
import com.wiscamp.ninechapters.panels.application.vo.MenuVO;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;


/**
 * 菜单服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/ControlPanels")
@Tag(name = "控制面板-菜单信息")
public class MenuController extends BaseController {

    private final MenuAppService menuAppService;

    public MenuController(MenuAppService menuAppService) {
        this.menuAppService = menuAppService;
    }

    @GetMapping("Menus/{menuId}")
    @Operation(summary = "通过编号获取菜单")
    @HandleResourceException
    public RestResponse<MenuVO> getMenu(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                        @PathVariable("menuId") long menuId) {
        return ResourceUtils.getById(request, response, menuId, menuAppService::getMenu);
    }


    @GetMapping("Applications/{appId}/Menus/Code/{menuCode}")
    @Operation(summary = "通过代码获取菜单")
    @HandleResourceException
    public RestResponse<MenuVO> getMenuByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                              @PathVariable("appId") long appId, @PathVariable("menuCode") String menuCode) {
        return ResourceUtils.getByIdAndText(request, response, appId, menuCode, menuAppService::getMenuByCode);
    }

    @GetMapping("Applications/{appId}/Menus/Name/{menuName}")
    @Operation(summary = "通过名称获取菜单列表")
    @HandleResourceException
    public RestResponse<Collection<MenuVO>> getMenusByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @PathVariable("appId") long appId, @PathVariable("menuName") String menuName) {
        return ResourceUtils.getListByIdAndText(request, response, appId, menuName, menuAppService::getMenusByName);
    }

    @GetMapping("Applications/{appId}/Menus/Group/{menuGroup}")
    @Operation(summary = "通过分组获取菜单列表")
    @HandleResourceException
    public RestResponse<Collection<MenuVO>> getMenusByGroup(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @PathVariable("appId") long appId, @PathVariable("menuGroup") String dictionarGroup) {
        return ResourceUtils.getListByIdAndText(request, response, appId, dictionarGroup, menuAppService::getMenusByGroup);
    }

    @PostMapping("Menus/Query")
    @Operation(summary = "分页获取菜单列表")
    @HandleResourceException
    public RestResponse<Collection<MenuVO>> getMenusByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, menuAppService::getMenusByQuery);
    }

    @PostMapping("Menus")
    @Operation(summary = "创建菜单")
    @HandleResourceException
    public RestResponse<MenuVO> createMenu(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreateMenuCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, menuAppService::createMenu);
    }


    @PutMapping("Menus")
    @Operation(summary = "更新菜单")
    @HandleResourceException
    public RestResponse<MenuVO> updateMenu(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody UpdateMenuCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, menuAppService::updateMenu);
    }

    @PutMapping("Menus/Actions/Delete")
    @Operation(summary = "通过编号删除菜单")
    @HandleResourceException
    public RestResponse<Boolean> deleteMenu(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, menuAppService::deleteMenu);
    }

    @PutMapping("Menus/Actions/Enable")
    @Operation(summary = "通过编号启用菜单")
    @HandleResourceException
    public RestResponse<Boolean> enableMenu(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, menuAppService::enableMenu);
    }

    @PutMapping("Menus/Actions/Disable")
    @Operation(summary = "通过编号禁用菜单")
    @HandleResourceException
    public RestResponse<Boolean> disableMenu(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, menuAppService::disableMenu);
    }
}
